//
//  GCMSearchViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/11/18.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMSearchViewController.h"
#import "HJDatePickerView.h"
#import "GCMSheBeiRequst.h"

@interface GCMSearchViewController ()<UIAlertViewDelegate>

@end

@implementation GCMSearchViewController{

    UIButton * _leftbutton;
    UIButton * _rightbutton;
    UIButton * _startTimeBtn;
    UIButton * _endTimeBtn;
    NSMutableArray *_houseArr;
    NSMutableArray *_houseNameArr;
    NSMutableArray *_houseIdArr;
    NSMutableArray *_typeArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self downLoadData];
//    [self createUI];
    
   
    
}
-(void)downLoadData{
    
    [GCMHostSearchHttpRequst postDataWithUserName:[GCMUserInfo instance].realName Success:^(id obj, NSError *err) {
        _houseArr  = obj;
        NSLog(@"小区%@",_houseArr);
        [GCMSheBeiRequst postScanDataSuccess:^(id obj, NSError *err) {
            _typeArr  = obj;
            NSLog(@"类型%@",_typeArr);
            [self createUI];
        }];
        
    }];
    

}
-(void)createUI{

    NSArray *arr = @[@"小区选择:",@"巡检类型:",@"开始时间:",@"结束时间:"];
    for (int i = 0; i < 4; i ++) {
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 64 + 10 +40 * i, (SCREEN_WIDTH - 20)/3, 30)];
        lab.text = arr[i];
//        lab.backgroundColor = [UIColor orangeColor];
        lab.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:lab];
    }
    UIButton *button  = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4, 170 + 80 , SCREEN_WIDTH / 2, 40)];
    button.backgroundColor = [UIColor colorWithRed:0.25 green:0.44 blue:0.63 alpha:1];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(onsubButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    //创建头部的下拉选择框
    _houseNameArr = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in _houseArr) {
        [_houseNameArr addObject:[dic valueForKey:@"name"]];
    }
    
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){0.81,0.81,0.81,1});
    _leftbutton = [[UIButton alloc] initWithFrame:CGRectMake( 10 + (SCREEN_WIDTH - 20)/3, 74, (SCREEN_WIDTH-20) /3 * 2, 30)];
    _rightbutton = [[UIButton alloc] initWithFrame:CGRectMake( 10 + (SCREEN_WIDTH - 20)/3, 114, (SCREEN_WIDTH-20) /3 * 2, 30)];
    _leftbutton.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    _rightbutton.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
    [_leftbutton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_leftbutton setTitle:_houseNameArr[0] forState:UIControlStateNormal];
    [_rightbutton setTitle:_typeArr[0] forState:UIControlStateNormal];
    [_leftbutton setBackgroundColor:[UIColor whiteColor]];
    [_rightbutton setBackgroundColor:[UIColor whiteColor]];
    [_rightbutton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    UIImageView *rimageViwe = [[UIImageView alloc]initWithFrame:CGRectMake(_rightbutton.bounds.size.width - 40, 0, 30, 30)];
    [rimageViwe setImage:[UIImage imageNamed:@"xiala"]];
    [_rightbutton addSubview:rimageViwe];
    UIImageView *limageViwe = [[UIImageView alloc]initWithFrame:CGRectMake(_rightbutton.bounds.size.width - 40, 0, 30, 30)];
    [limageViwe setImage:[UIImage imageNamed:@"xiala"]];
    [_leftbutton addSubview:limageViwe];
    [_leftbutton.layer setBorderWidth:1.f];
    [_leftbutton.layer setBorderColor:color];
    [_leftbutton.layer setCornerRadius:0];
    [_rightbutton.layer setBorderColor:color];
    [_rightbutton.layer setBorderWidth:1.f];
    [_rightbutton.layer setCornerRadius:0];
    [_leftbutton addTarget:self action:@selector(onLeftbutton) forControlEvents:UIControlEventTouchUpInside];
    [_rightbutton addTarget:self action:@selector(onRightbutton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_leftbutton];
    [self.view addSubview:_rightbutton];
    
//    日期
    NSString *timestr = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSArray *arrayt1 = [timestr componentsSeparatedByString:@" "];
    _startTimeBtn = [[UIButton alloc] initWithFrame:CGRectMake( 10 + (SCREEN_WIDTH - 20)/3, 154, (SCREEN_WIDTH-20) /3 * 2, 30)];
    _endTimeBtn = [[UIButton alloc] initWithFrame:CGRectMake( 10 + (SCREEN_WIDTH - 20)/3, 194, (SCREEN_WIDTH-20) /3 * 2, 30)];
    _startTimeBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    _endTimeBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
    [_startTimeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_startTimeBtn setTitle:arrayt1[0] forState:UIControlStateNormal];
    [_endTimeBtn setTitle:arrayt1[0] forState:UIControlStateNormal];
    [_startTimeBtn setBackgroundColor:[UIColor whiteColor]];
    [_endTimeBtn setBackgroundColor:[UIColor whiteColor]];
    [_endTimeBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    UIImageView *uimageViwe = [[UIImageView alloc]initWithFrame:CGRectMake(_endTimeBtn.bounds.size.width - 40, 0, 30, 30)];
    [uimageViwe setImage:[UIImage imageNamed:@"xiala"]];
    [_endTimeBtn addSubview:uimageViwe];
    UIImageView *dimageViwe = [[UIImageView alloc]initWithFrame:CGRectMake(_endTimeBtn.bounds.size.width - 40, 0, 30, 30)];
    [dimageViwe setImage:[UIImage imageNamed:@"xiala"]];
    [_startTimeBtn addSubview:dimageViwe];
    [_startTimeBtn.layer setBorderWidth:1.f];
    [_startTimeBtn.layer setBorderColor:color];
    [_startTimeBtn.layer setCornerRadius:0];
    [_endTimeBtn.layer setBorderColor:color];
    [_endTimeBtn.layer setBorderWidth:1.f];
    [_endTimeBtn.layer setCornerRadius:0];
    [_startTimeBtn addTarget:self action:@selector(onyyshBtn1Click) forControlEvents:UIControlEventTouchUpInside];
    [_endTimeBtn addTarget:self action:@selector(onyyshBtn2Click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_startTimeBtn];
    [self.view addSubview:_endTimeBtn];

}
-(void)onsubButtonClick{

    NSMutableArray *arr = [[NSMutableArray alloc]init];
    for (NSDictionary *d in _houseArr) {
        if ([[d valueForKey:@"name"] isEqualToString:_leftbutton.titleLabel.text]) {
            [arr addObject:[d valueForKey:@"id"]];
        }
    }
    [arr addObject:_rightbutton.titleLabel.text];
    [arr addObject:_startTimeBtn.titleLabel.text];
    [arr addObject:_endTimeBtn.titleLabel.text];
    self.returnArrayBlock(arr);
    [self.navigationController popViewControllerAnimated:YES];
}

//获取时间1
-(void)onyyshBtn1Click{
    
    HJDatePickerView * datePickerView = [[HJDatePickerView alloc]init];
    [datePickerView createDatePickerMode:UIDatePickerModeDate GetTime:^(NSString *time) {
        NSString *nstring = time;
        NSArray *array = [nstring componentsSeparatedByString:@" "];
        [_startTimeBtn setTitle:array[0] forState:UIControlStateNormal];
    }];
    
}
//获取时间2
-(void)onyyshBtn2Click{
    
    HJDatePickerView * datePickerView = [[HJDatePickerView alloc]init];
    [datePickerView createDatePickerMode:UIDatePickerModeDate GetTime:^(NSString *time) {
        NSString *nstring = time;
        NSArray *array = [nstring componentsSeparatedByString:@" "];
        [_endTimeBtn setTitle:array[0] forState:UIControlStateNormal];
    }];
}

#pragma mark--展示下拉菜单
-(void)onLeftbutton{
    
    __weak typeof(self)weakSelf = self;
    if (!_leftmuen) {
        
        _leftmuen=[[SCXCreateAlertView alloc]initWithNameArray:_houseNameArr andMenuOrigin:CGPointMake(0,114) andMenuWidth:SCREEN_WIDTH andHeight:40 andLayer:0 andTableViewBackGroundColor:[UIColor clearColor] andIsSharp:YES andType:kNavigationPop];
        _leftmuen.delegate = weakSelf;
        _leftmuen.tableViewDelegate=self;
        _leftmuen.dismiss=^(){
            [weakSelf.leftmuen removeFromSuperview];
            _leftmuen=nil;
        };
        [self.view addSubview:_leftmuen];
    }
    else{
        [_leftmuen dismissWithCompletion:^(SCXCreateAlertView *create){
            [_leftmuen removeFromSuperview];
            _leftmuen = nil;
        }];
    }
}

#pragma mark--自定义下拉菜单代理方法,点击下拉菜单某一行时候对应的响应时间，点击完成后试图要消失，用一个block通知；
-(void)tableViewDidSelectRowAtIndexPath:(NSIndexPath *)indexPath andPopType:(popType)popViewType{
    
    //改变导航栏文字
    if (popViewType == kNavigationPop) {
        [_leftbutton setTitle:_houseNameArr[indexPath.row] forState:UIControlStateNormal];
    }
    [_leftmuen dismissWithCompletion:nil];//点击完成后视图消失;
    
}
#pragma mark---同上
-(void)onRightbutton{
    __weak typeof(self)weakSelf = self;
    if (!_rightmuen) {
        
        _rightmuen=[[SCXCreateAlertView2 alloc]initWithNameArray:_typeArr andMenuOrigin:CGPointMake(0,154 ) andMenuWidth:SCREEN_WIDTH andHeight:40 andLayer:0 andTableViewBackGroundColor:[UIColor clearColor] andIsSharp:YES andType:kNavigationPop2];
        _rightmuen.delegate = weakSelf;
        _rightmuen.tableViewDelegate=self;
        _rightmuen.dismiss=^(){
            [weakSelf.rightmuen removeFromSuperview];
            _rightmuen=nil;
        };
        [self.view addSubview:_rightmuen];
    }
    else{
        [_rightmuen dismissWithCompletion:^(SCXCreateAlertView2 *create) {
            [_rightmuen removeFromSuperview];
            _rightmuen = nil;
        }];
    }
}

-(void)secondtableViewDidSelectRowAtIndexPath:(NSIndexPath *)indexPath andPopType:(popType2)popViewType{
    //改变导航栏文字
    if (popViewType==kNavigationPop2) {
        [_rightbutton setTitle:_typeArr[indexPath.row] forState:UIControlStateNormal];
    }
    [_rightmuen dismissWithCompletion:nil];//点击完成后视图消失;
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
